﻿namespace HuaWei
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Reflection;
    using System.Windows.Forms;

    public class TypeTransfer
    {
        public static bool ConvertToBoolean(object oValue)
        {
            if (Convert.IsDBNull(oValue) || (oValue == null))
            {
                return false;
            }
            return Convert.ToBoolean(oValue);
        }

        public static decimal ConvertToDecimal(object oValue)
        {
            if (Convert.IsDBNull(oValue) || (oValue == null))
            {
                return 0M;
            }
            return Convert.ToDecimal(oValue);
        }

        public static double ConvertToDouble(object oValue)
        {
            if (Convert.IsDBNull(oValue) || (oValue == null))
            {
                return 0.0;
            }
            return Convert.ToDouble(oValue);
        }

        public static short ConvertToInt16(object oValue)
        {
            if (Convert.IsDBNull(oValue) || (oValue == null))
            {
                return 0;
            }
            return Convert.ToInt16(oValue);
        }

        public static int ConvertToInt32(object oValue)
        {
            if (Convert.IsDBNull(oValue) || (oValue == null))
            {
                return 0;
            }
            return Convert.ToInt32(oValue);
        }

        public static long ConvertToInt64(object oValue)
        {
            if (Convert.IsDBNull(oValue) || (oValue == null))
            {
                return 0L;
            }
            return Convert.ToInt64(oValue);
        }

        public static Hashtable GetDataMapText()
        {
            SortHashtable hashtable = new SortHashtable();
            hashtable.Add(1, "文本");
            hashtable.Add(2, "长文本");
            hashtable.Add(3, "浮点数");
            hashtable.Add(4, "布尔");
            hashtable.Add(5, "日期");
            hashtable.Add(6, "长日期");
            hashtable.Add(7, "整数");
            hashtable.Add(8, "文件");
            return hashtable;
        }

        public static DbType GetDbType(string typeString)
        {
            return GetDbType(System.Type.GetType(typeString));
        }

        public static DbType GetDbType(System.Type type)
        {
            if (type == typeof(bool))
            {
                return DbType.Boolean;
            }
            if (type == typeof(byte))
            {
                return DbType.Byte;
            }
            if (type == typeof(byte[]))
            {
                return DbType.Binary;
            }
            if (type == typeof(DateTime))
            {
                return DbType.DateTime;
            }
            if (type == typeof(decimal))
            {
                return DbType.Decimal;
            }
            if (type == typeof(double))
            {
                return DbType.Double;
            }
            if (type == typeof(float))
            {
                return DbType.Single;
            }
            if (type == typeof(Guid))
            {
                return DbType.Guid;
            }
            if (type == typeof(short))
            {
                return DbType.Int16;
            }
            if (type == typeof(int))
            {
                return DbType.Int32;
            }
            if (type == typeof(long))
            {
                return DbType.Int64;
            }
            if (type == typeof(object))
            {
                return DbType.Object;
            }
            if (type == typeof(string))
            {
                return DbType.String;
            }
            if (type == typeof(TimeSpan))
            {
                return DbType.Time;
            }
            if (type == typeof(ushort))
            {
                return DbType.UInt16;
            }
            if (type == typeof(uint))
            {
                return DbType.UInt32;
            }
            if (type != typeof(ulong))
            {
                throw new Exception("类型" + type.ToString() + "未知！");
            }
            return DbType.UInt64;
        }

        public static System.Type GetType(string className)
        {
            System.Type type = null;
            try
            {
                type = System.Type.GetType(className);
                if (type == null)
                {
                    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                    for (int i = 0; i < assemblies.Length; i++)
                    {
                        try
                        {
                            string fullName = assemblies[i].FullName;
                            string str2 = className.Substring(0, className.LastIndexOf('.'));
                            if (fullName.Contains(str2))
                            {
                                type = assemblies[i].GetType(className);
                                if (type != null)
                                {
                                    return type;
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
                return type;
            }
            catch
            {
            }
            return type;
        }

        public static int MonthToQuarter(int month)
        {
            int num = 0;
            if ((month >= 1) && (month <= 3))
            {
                return 1;
            }
            if ((month >= 4) && (month <= 6))
            {
                return 2;
            }
            if ((month >= 7) && (month <= 9))
            {
                return 3;
            }
            if ((month >= 10) && (month <= 12))
            {
                num = 4;
            }
            return num;
        }

        public static int[] QuarterToMonth(int quarter)
        {
            int[] numArray = new int[2];
            switch (quarter)
            {
                case 1:
                    numArray[0] = 1;
                    numArray[1] = 3;
                    return numArray;

                case 2:
                    numArray[0] = 4;
                    numArray[1] = 6;
                    return numArray;

                case 3:
                    numArray[0] = 7;
                    numArray[1] = 9;
                    return numArray;

                case 4:
                    numArray[0] = 10;
                    numArray[1] = 12;
                    return numArray;
            }
            return numArray;
        }

        public static object StrongValue(string value, string valueTypeString)
        {
            System.Type type;
            if (valueTypeString.ToLower() == "system.string")
            {
                return value;
            }
            object obj3 = new object();
            if (!string.IsNullOrEmpty(value))
            {
                if (valueTypeString.ToLower() == "anchorstyles")
                {
                    AnchorStyles none = AnchorStyles.None;
                    if (value.ToLower().IndexOf("top") >= 0)
                    {
                        none |= AnchorStyles.Top;
                    }
                    if (value.ToLower().IndexOf("bottom") >= 0)
                    {
                        none |= AnchorStyles.Bottom;
                    }
                    if (value.ToLower().IndexOf("left") >= 0)
                    {
                        none |= AnchorStyles.Left;
                    }
                    if (value.ToLower().IndexOf("right") >= 0)
                    {
                        none |= AnchorStyles.Right;
                    }
                    return none;
                }
                if (valueTypeString.ToLower() == "system.windows.forms.padding")
                {
                    PaddingConverter converter = new PaddingConverter();
                    string[] strArray = value.Split(new char[] { ',', '=', '}' });
                    if (strArray.Length == 9)
                    {
                        string text = string.Format("{0},{1},{2},{3}", new object[] { strArray[1], strArray[3], strArray[5], strArray[7] });
                        obj3 = converter.ConvertFromString(text);
                    }
                    else if (strArray.Length == 4)
                    {
                        obj3 = converter.ConvertFromString(value);
                    }
                    else
                    {
                        obj3 = new Padding(0);
                    }
                    return obj3;
                }
                type = GetType(valueTypeString);
                if ((type != null) && type.BaseType.Equals(typeof(Enum)))
                {
                    obj3 = Enum.Parse(type, value);
                }
                else if ((type != null) && type.Equals(typeof(Color)))
                {
                    obj3 = TypeDescriptor.GetConverter(type).ConvertFromString(value);
                }
                else
                {
                    obj3 = type.GetMethod("Parse", new System.Type[] { typeof(string) }).Invoke(null, new object[] { value });
                }
                return obj3;
            }
            if (valueTypeString.ToLower() == "anchorstyles")
            {
                return AnchorStyles.None;
            }
            if (valueTypeString.ToLower() == "system.windows.forms.padding")
            {
                return new Padding(0);
            }
            if (valueTypeString.ToLower() == "system.drawing.color")
            {
                return SystemColors.Control;
            }
            type = System.Type.GetType(valueTypeString);
            if (type == typeof(int))
            {
                obj3 = 0;
            }
            else
            {
                if (type == typeof(decimal))
                {
                    return 0M;
                }
                if (type == typeof(double))
                {
                    return 0.0;
                }
                if (type == typeof(long))
                {
                    return 0L;
                }
                if (type == typeof(DateTime))
                {
                    return new DateTime();
                }
                if (type == typeof(bool))
                {
                    return false;
                }
                if (type == typeof(char))
                {
                    obj3 = '\0';
                }
                else
                {
                    type = GetType(valueTypeString);
                    if ((type != null) && type.BaseType.Equals(typeof(Enum)))
                    {
                        obj3 = Enum.Parse(type, "0");
                    }
                }
            }
            return obj3;
        }

        public static Cursor TransferForCursors(string cursor)
        {
            switch (cursor)
            {
                case "Arrow":
                    return Cursors.Arrow;

                case "Cross":
                    return Cursors.Cross;

                case "Default":
                    return Cursors.Default;

                case "Hand":
                    return Cursors.Hand;

                case "Help":
                    return Cursors.Help;

                case "HSplit":
                    return Cursors.HSplit;

                case "IBeam":
                    return Cursors.IBeam;

                case "No":
                    return Cursors.No;

                case "NoMove2D":
                    return Cursors.NoMove2D;

                case "NoMoveHoriz":
                    return Cursors.NoMoveHoriz;

                case "NoMoveVert":
                    return Cursors.NoMoveVert;

                case "SizeAll":
                    return Cursors.SizeAll;

                case "SizeNESW":
                    return Cursors.SizeNESW;

                case "SizeNS":
                    return Cursors.SizeNS;

                case "SizeNWSE":
                    return Cursors.SizeNWSE;

                case "SizeWE":
                    return Cursors.SizeWE;

                case "UpArrow":
                    return Cursors.UpArrow;

                case "VSplit":
                    return Cursors.VSplit;

                case "WaitCursor":
                    return Cursors.WaitCursor;
            }
            return Cursors.Default;
        }

        public static System.Type TransferForNet(int dbValue)
        {
            if ((dbValue != 1) && (dbValue != 2))
            {
                if (dbValue == 3)
                {
                    return typeof(decimal);
                }
                if (dbValue == 4)
                {
                    return typeof(bool);
                }
                if ((dbValue == 5) || (dbValue == 6))
                {
                    return typeof(DateTime);
                }
                if (dbValue == 7)
                {
                    return typeof(int);
                }
                if (dbValue == 8)
                {
                    return typeof(byte[]);
                }
            }
            return typeof(string);
        }

        public static int TransferForNet(System.Type TypeValue)
        {
            if (TypeValue != typeof(string))
            {
                if (TypeValue == typeof(decimal))
                {
                    return 3;
                }
                if (TypeValue == typeof(bool))
                {
                    return 4;
                }
                if (TypeValue == typeof(DateTime))
                {
                    return 5;
                }
                if (TypeValue == typeof(int))
                {
                    return 7;
                }
                if (TypeValue == typeof(byte[]))
                {
                    return 8;
                }
            }
            return 1;
        }
    }
}

